package com.kyriec.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kyriec.Util.EResult;
import com.kyriec.entity.Users;
import com.kyriec.mapper.UsersMapper;
import com.kyriec.params.LoginParam;
import com.kyriec.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 用户(Users)表服务实现类
 *
 * @author KyrieC
 * @since 2023-11-25 11:58:16
 */
@Service("usersService")
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {

    @Autowired
    private UsersMapper usersMapper;

    @Override
    public EResult<Users> login(LoginParam loginParam) {
        LambdaQueryWrapper<Users> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Users::getUsername, loginParam.getUsername()).eq(Users::getPassword, loginParam.getPassword());
        Users users = usersMapper.selectOne(queryWrapper);
        if(users == null){
            //登录失败
            return EResult.error("用户名或密码错误");
        }
        //登录成功
        return EResult.success(users);
    }
    @Override
    public EResult register(Users users) {
        save(users);
        users.setPassword("");
        return EResult.success(users);
    }
}

